<script lang="ts" setup>
import { ref } from 'vue'
import { toNumber } from 'lodash-es'
import { ElMessage } from 'element-plus'
import { GetData, patchListSort } from '@/api/modules/memberproduct'
import PageNumber from '@/store/modules/PageNumberCache'

// const imgDomain = 'https://img1.eprhan.cc'
const router = useRouter()
const PageNumberCache = PageNumber()

const findFrom = ref({
  WxTemplateId: '',
  WxTemplateTitle: '',
  Enable: '',
})
const dataList: any = reactive({
  list: [],
})
const loading = ref(true)
const total = ref(0)
let PageSize = 20

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.WxTemplateId && ({ WxTemplateId: findFrom.value.WxTemplateId })),
    ...(findFrom.value.WxTemplateTitle && ({ WxTemplateTitle: findFrom.value.WxTemplateTitle })),
    ...(findFrom.value.Enable && ({ Enable: findFrom.value.Enable })),
  }
  await GetData(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
  })
  loading.value = false
}
// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 添加
function AddData() {
  router.push({
    name: 'memberproductDetail',
  })
}

// 编辑
function Redact(id: number) {
  router.push({
    name: 'memberproductDetail',
    query: {
      id,
    },
  })
}

// 修改排序
function SortModification(message: any) {
  const params = {
    id: message.row.id,
    sort: toNumber(message.row.sort),
  }
  patchListSort(params).then((res: any) => {
    if (res.data) {
      ElMessage.success('修改成功。')
      GetDataList(PageNumberCache.GetMemberProductPage(), PageSize)
    }
    else {
      ElMessage.error('修改失败。')
      GetDataList(PageNumberCache.GetMemberProductPage(), PageSize)
    }
  })
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}
function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
  PageNumberCache.SetMemberProductPage(val)
}

const nowPageNum = computed(() => {
  return PageNumberCache.GetMemberProductPage()
})

onMounted(async () => {
  GetDataList(PageNumberCache.GetMemberProductPage())
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="模板ID">
                <el-input v-model="findFrom.WxTemplateId" placeholder="请输入模板ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="模板标题">
                <el-input v-model="findFrom.WxTemplateTitle" placeholder="请输入模板标题" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="是否启用">
                <el-select v-model="findFrom.Enable" placeholder="请选择">
                  <el-option label="是" :value="true" />
                  <el-option label="否" :value="false" />
                </el-select>
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div>
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="id" label="ID" width="100" />
        <el-table-column align="center" prop="productId" label="商品ID" width="100" />
        <el-table-column header-align="center" prop="productName" label="商品名称" width="400" />
        <el-table-column align="center" prop="payType" label="支付类型" width="120">
          <template #default="scope">
            <el-tag type="primary">
              {{ scope.row.payType === 1 ? '兑换' : scope.row.payType === 2 ? '组合' : '未知' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="productFlag" label="商品类型" width="200">
          <template #default="scope">
            <el-tag type="primary">
              {{ scope.row.productFlag === 2 ? '积分商品' : scope.row.productFlag === 3 ? '秒杀商品' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="saleScore" label="积分数量" width="200" />
        <el-table-column align="center" prop="secTime" label="开始时间" width="200" />
        <el-table-column align="center" prop="secEndTime" label="结束时间" width="200" />
        <el-table-column align="center" prop="sort" label="排序" fixed="right" width="160">
          <template #default="scope">
            <el-input v-model="scope.row.sort" style="width: 120px;">
              <template #append>
                <el-button type="primary" size="small" @click="SortModification(scope)">
                  修改
                </el-button>
              </template>
            </el-input>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" min-width="200">
          <template #default="scope">
            <el-button type="primary" @click="Redact(scope.row.id)">
              编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;

  div {
    text-align: left;
  }
}
</style>
